/**
 * Created by KHACHUNG on 4/23/14.
 */

$().ready(function() {

    // TinyMCE
    tinyMCE.init({
        // General options
        mode : "exact",
        elements : "elm3",
        theme : "advanced",
        skin : "o2k7",
        skin_variant : "silver",
        plugins : "lists,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups,autosave",
        // Theme options
        theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,styleselect,formatselect,fontselect,fontsizeselect",
        theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
        theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
        theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak,restoredraft",
        theme_advanced_toolbar_location : "top",
        theme_advanced_toolbar_align : "left",
        theme_advanced_statusbar_location : "bottom",
        theme_advanced_resizing : true,
        // Drop lists for link/image/media/template dialogs
        template_external_list_url : "lists/template_list.js",
        external_link_list_url : "lists/link_list.js",
        external_image_list_url : "lists/image_list.js",
        media_external_list_url : "lists/media_list.js",
    });


    // validate the comment form when it is submitted
    // validate signup form on keyup and submit
    $("#validateForm").validate({
        rules: {
            cname: {
                required: true,
            },
            cname2: {
                required: true,
                minlength: 3
            },
            email: {
                required: true,
                email: true
            },
            password: {
                required: true,
                minlength: 5
            },
            confirm_password: {
                required: true,
                minlength: 5,
                equalTo: "#password"
            },
            topic: {
                required: "#newsletter:checked",
                minlength: 2
            },
            agree: "required",
            radios: "required"
        },
        messages: {
            firstname: "Please enter your firstname",
            lastname: "Please enter your lastname",
            username: {
                required: "Please enter a username",
                minlength: "Your username must consist of at least 2 characters"
            },
            password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long"
            },
            confirm_password: {
                required: "Please provide a password",
                minlength: "Your password must be at least 5 characters long",
                equalTo: "Please enter the same password as above"
            },
            email: "Please enter a valid email address",
            agree: "Please accept our policy"
        }
    });
    // propose username by combining first- and lastname
    $("#username").focus(function() {
        var firstname = $("#firstname").val();
        var lastname = $("#lastname").val();
        if(firstname && lastname && !this.value) {
            this.value = firstname + "." + lastname;
        }
    });
    //code to hide topic selection, disable for demo
    var newsletter = $("#newsletter");
    // newsletter topics are optional, hide at first
    var inital = newsletter.is(":checked");
    var topics = $("#newsletter_topics")[inital ? "removeClass" : "addClass"]("gray");
    var topicInputs = topics.find("input").attr("disabled", !inital);
    // show when newsletter is checked
    newsletter.click(function() {
        topics[this.checked ? "removeClass" : "addClass"]("gray");
        topicInputs.attr("disabled", !this.checked);
    });
});

//Forms
$(document).ready(function () {

    $('textarea.autogrow').autogrow();
    var elem = $("#chars");
    $("#text").limiter(100, elem);
    // Mask plugin http://digitalbush.com/projects/masked-input-plugin/
    $("#mask-phone").mask("(999) 999-9999");
    $("#mask-date").mask("99-99-9999");
    $("#mask-int-phone").mask("+33 999 999 999");
    $("#mask-tax-id").mask("99-9999999");
    $("#mask-percent").mask("99%");
    // Editor plugin https://github.com/jhollingworth/bootstrap-wysihtml5/
    $('#editor1').wysihtml5({
        "image": false,
        "link": false
    });
    // Chosen select plugin
    $(".chzn-select").chosen({
        disable_search_threshold: 10
    });
    // Datepicker
    $('#datepicker1').datepicker({
        format: 'mm-dd-yyyy'
    });
    $('#datepicker2').datepicker();
    $('.colorpicker').colorpicker()
    $('#colorpicker3').colorpicker();
});

